package com.sample.tdf.api;

import cn.com.taiji.common.api.BaseTreeController;
import cn.com.taiji.common.dto.ResultDTO;
import com.sample.tdf.domain.Department;
import com.sample.tdf.dto.DepartmentDTO;
import com.sample.tdf.dto.EmployeeDTO;
import com.sample.tdf.dto.SchoolGradeDTO;
import com.sample.tdf.service.IDepartmentService;
import com.sample.tdf.service.IEmployeeDepartmentService;
import com.sample.tdf.service.IEmployeeService;
import com.sample.tdf.service.ISchoolGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"402.部门的接口操作"})
@RestController
@RequestMapping(value = "/homework/department")
public class DepartmentController extends BaseTreeController<IDepartmentService, DepartmentDTO> {

    @Autowired
    IEmployeeService iEmployeeService;

    @Autowired
    IEmployeeDepartmentService iEmployeeDepartmentService;

    @ApiOperation(value = "根据部门id获取所有员工的树状结构")
    @GetMapping("/getemployee")
    public ResultDTO getemployee(@RequestParam String departmentId){
        //根据id获取部门实体
        DepartmentDTO departmentDTO = iBaseService.findById(departmentId);

        //获取部门id下的所有儿子节点的部门list
        List<DepartmentDTO> departmentDTOList = iBaseService.findChildrenByParentId(departmentId);

        //循环遍历素有的部门儿子节点的id
        departmentDTOList.forEach((item) -> {
            //级联表是部门ID；员工ID
            List<String> employeeIds = iEmployeeDepartmentService.findSecondIdsByFirstId(item.getId());
            List<EmployeeDTO> employeeDTOList = iEmployeeService.findByIds(employeeIds);
            item.setEmployeeList(employeeDTOList);
        });
        List<DepartmentDTO> data = iBaseService.listToTree(departmentDTOList, departmentDTO.getParentId());
        return new ResultDTO(data);
    }
}
